﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //Dictionary<int, string> dic = new Dictionary<int, string>();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            BindGv();
           
            //for (int i = 0; i <=30; i++)
            //{
            //    string date = DateTime.Now.AddDays(i).ToString("yyy-MM-dd");
            //    dic.Add(i, date);                
            //}
            //BindingSource bs = new BindingSource();
            //bs.DataSource = dic;
            //this.comboBox1.DataSource = bs;
            //this.comboBox1.DisplayMember = "value"; //显示列
            //this.comboBox1.ValueMember = "key";     //隐藏的列
            ////this.comboBox1.SelectedIndex = 0;
        }
        private void BindGv()
        {
            string sql = "select * from HomeInfo";
            DataSet ds = dbHelper.GetData(sql);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //从前台获取用户输入的数据
            string hName = this.txtHName.Text;
            string price = this.txtPrice.Text;
            string day = this.txtDay.Text;
            string name = this.txtName.Text;
            string tel = this.txtTel.Text;
            //int num = Convert.ToInt32(this.comboBox1.SelectedValue);
            //string date = dic[num];
            string date2 = this.dateTimePicker1.Value.ToShortDateString();
            //判断以上数据不能为空
            if (string.IsNullOrEmpty(hName))
            {
                MessageBox.Show("数据不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(price))
            {
                MessageBox.Show("数据不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(day))
            {
                MessageBox.Show("数据不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("数据不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(tel))
            {
                MessageBox.Show("数据不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(date2))
            {
                MessageBox.Show("数据不能为空！");
                return;
            }
            string sql = $"insert into HomeInfo values('{hName}',{price},'{date2}',{day},'{name}','{tel}')";
            bool boo = dbHelper.ExecuteNonQuery(sql);
            if (boo)
            {
                BindGv();
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
        }
    }
}
